<?php
/**
 * @package		简易CMS
 * @subpackage	CSS、JS压缩输出处理类
 * @version		$Id: class_mergejscss.php 23 2012-06-11 18:04:03Z htmambo@gmail.com $
 * @author		Hoping
 * @copyright	Copyright (C) 2010 Hoping Software Studio.
 */

class jsCssCompressor {
	function makeJs($basUrl,$joomlaRoot,$pathes,$to,$isGZ,$jqueryNoConflict,$jquery) {
		if(file_exists($to)) unlink($to);
		$fp=fopen($to,"wb");
		$data='';
		$header='';
		if($isGZ){
			$header='<?php '
			.'ob_start("ob_gzhandler");'
			.'header("Cache-Control: public");'
			.'header("Pragma: cache");'
			.'$offset = 60*60*24*60;'
			.'$ExpStr = "Expires: ".gmdate("D, d M Y H:i:s",time() + $offset)." GMT";'
			.'$LmStr = "Last-Modified: ".gmdate("D, d M Y H:i:s",filemtime(__FILE__))." GMT";'
			.'header($ExpStr);'
			.'header($LmStr);'
			.'header("Content-Type: text/javascript; charset: UTF-8");'
			.'?>'
			.' ';
		}
		foreach((array)$pathes as $v) {
			if(file_exists($v)){
				$content=file_get_contents($v);
				if($jqueryNoConflict&&$jquery==$v){
					$content.="\n jQuery.noConflict();\n";
				}
				$data .=$this->clear_js($content);
			}else{
				echo('File not exist'.$v);
			}
		}
		fwrite($fp,$data );
		fclose($fp);
		if(file_exists($to)) {return true;}else{return false;}
	}
	function makeJsold($basUrl,$joomlaRoot,$pathes,$to,$isGZ,$jqueryNoConflict,$jquery) {
		if(file_exists($to)) unlink($to);
		$fp=fopen($to,"wb");
		$data='';
		foreach((array)$pathes as $v) {
			if(file_exists($v)){
				$content=file_get_contents($v);
				if($jqueryNoConflict&&$jquery==$v){
					$content.="\n jQuery.noConflict();\n";
				}
				$data .=$this->clear_js($content);
			}else{
				echo('File not exist'.$v);
			}
		}
		if($isGZ){
			fwrite($fp,gzencode($data));
		}else{
			fwrite($fp,$data );
		}
		fclose($fp);
		if(file_exists($to)) {return true;}else{return false;}
	}

	function makeCss($basUrl,$joomlaRoot,$pathes,$to,$isGZ) {
		if(file_exists($to)) unlink($to);
		$fp=fopen($to,"wb");
		$data='';
		if($isGZ){
			$data='<?php '
			.'ob_start("ob_gzhandler");'
			.'header("Cache-Control: public");'
			.'header("Pragma: cache");'
			.'$offset = 60*60*24*60;'
			.'$ExpStr = "Expires: ".gmdate("D, d M Y H:i:s",time() + $offset)." GMT";'
			.'$LmStr = "Last-Modified: ".gmdate("D, d M Y H:i:s",filemtime(__FILE__))." GMT";'
			.'header($ExpStr);'
			.'header($LmStr);'
			.'header("Content-Type: text/css; charset: UTF-8");'
			.'?>'
			.' ';
		}
		foreach((array)$pathes as $v) {
			if(file_exists($v)){
				$data.=$this->loadcss($basUrl,$joomlaRoot,$v);
			}else{
				echo('File not exist'.$v);
			}
		}

		fwrite($fp,$data );

		fclose($fp);
		if(file_exists($to)) {return true;}else{return false;}
	}
	function makeCssOld($basUrl,$joomlaRoot,$pathes,$to,$isGZ) {
		if(file_exists($to)) unlink($to);
		$fp=fopen($to,"wb");
		$data='';
		foreach((array)$pathes as $v) {
			if(file_exists($v)){
				$data.=$this->loadcss($basUrl,$joomlaRoot,$v);

			}else{
				echo('File not exist'.$v);
			}
		}
		if($isGZ){
			fwrite($fp,gzencode($data));
		}else{
			fwrite($fp,$data );
		}
		fclose($fp);
		if(file_exists($to)) {return true;}else{return false;}
	}

	function clear_js($data) {
		return $data;
	}
	function loadcss($basUrl,$joomlaRoot,$file) {
		$data=file_get_contents($file);
		if(false){
		 $data = preg_replace('<
        \s*([@{}:;,]|\)\s|\s\()\s* |
        /\*([^*\\\\]|\*(?!/))+\*/ |
        [\n\r]
        >x', '\1', $data);
		}

		$base=basePathForFile($joomlaRoot,$basUrl,$file);
		echo($base);
		build_css_path(NULL, $base);
		//handle urls in css
		$data = preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', 'build_css_path', $data);
		return $data;
	}
}
//end class
function basePathForFile($joomlaRoot,$basUrl,$file=NULL){
	static $_joomlaRoot;
	static $_basUrl;
	if(isset($joomlaRoot)){
		$_joomlaRoot=$joomlaRoot;
		$_basUrl=$basUrl;

	}
	$base=str_replace($_joomlaRoot,$_basUrl,dirname($file)).'/';
	$base=str_replace("\\\\\\",'/',$base);
	$base=str_replace("///",'/',$base);
	$base=str_replace("/\\/",DS,$base);
	$base=str_replace("\\/\\",DS,$base);
	$base=str_replace("\\",'/',$base);
	$base=str_replace("\\\\",'/',$base);
	$base=str_replace("\\/",'/',$base);
	$base=str_replace("/\\",'/',$base);
	$base=str_replace("//",'/',$base);
	$base=str_replace("/",'/',$base);
	$base=str_replace('http:/','http://',$base);
	$base=str_replace('https:/','https://',$base);
	return $base;
}
function  build_css_path($matches, $base = NULL) {
	static $_base;
	// Store base path for preg_replace_callback.
	if (isset($base)) {
		$_base = $base;
	}
	// Prefix with base and remove '../' segments where possible.
	$path = $_base . $matches[1];
	$last = '';
	while ($path != $last) {
		$last = $path;
		$path = preg_replace('`(^|/)(?!\.\./)([^/]+)/\.\./`', '$1', $path);
	}
	return 'url('. $path .')';
}

function replaceCss($matches,$exclude = NULL){

	static $_exclude;
	// Store exclude css for preg_replace_callback.
	if (isset($exclude)) {
		$_exclude = $exclude;
	}else if(isset($_exclude)){
		$cssRegex="/([^\"\'=]+\.(css))[\"\']/i";
		preg_match_all($cssRegex, $matches[0], $m);
		if(isset($m[1])&&count($m[1])){
			$cssFile=$m[1][0];
			if(count($_exclude)){
				foreach((array)$_exclude as $exd){
					//echo $cssFile.'======>'.$exd.'<br>';
					if($exd&&endwith($cssFile, $exd)){
						return $matches[0];
					}
				}
			}
			$ignore= count($m[0])&&endwith( $cssFile,'.css')&&!endwith( $cssFile,'.css.php');
			if($ignore){
				return ' ';
			} else{
				return $matches[0];
			}
		}else{
			return $matches[0];
		}
	}

}

function replaceJs($matches,$exclude = NULL){
	static $_exclude;
	// Store exclude javascripts for preg_replace_callback.
	if (isset($exclude)) {
		$_exclude = $exclude;
	}else if(isset($_exclude)){
		$jsRegex="/src=[\"\']([^\"\']+)[\"\']/i";
		preg_match_all($jsRegex, $matches[0], $m);
		if(isset($m[1])&&count($m[1])){
			$scriptFile=$m[1][0];
			if(count($_exclude)){
				foreach((array)$_exclude as $exd){
					if($exd&&endwith($scriptFile, $exd)){
						return $matches[0];
					}
				}
			}

			$ignore= count($m[0])&&endwith( $scriptFile,'.js')&&!endwith( $scriptFile,'.js.php');
			if($ignore){
				return ' ';
			} else{
				return $matches[0];
			}
		}else{
			return $matches[0];
		}
	}
}

function endwith($FullStr, $EndStr)  {
	$StrLen = strlen($EndStr);
	$FullStrEnd = substr($FullStr, strlen($FullStr) - $StrLen);
	if($FullStrEnd == $EndStr){
		return true;
	}
	return false;
}

function fileName($whole){
	$file=strrchr($whole,'/');
	$file=substr($file,1);
	if(isset($file)){
		return trim($file);
	}else{
		return $whole;
	}
}

function replaceSperator($link){
	$link=str_replace("\\\\",DS,$link);
	$link=str_replace("/\\/",DS,$link);
	$link=str_replace("\\/\\",DS,$link);
	$link=str_replace("\\",DS,$link);
	$link=str_replace("//",DS,$link);
	$link=str_replace("/",DS,$link);
	return $link;
}
